<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>品牌数据</title>

    <style>
        .el-table .warning-row {
            background: oldlace;
        }

        .el-table .success-row {
            background: #f0f9eb;
        }
    </style>
</head>
<body>
<div id="app">
    <!--搜索表单组件-->
    <el-form :inline="true" :model="brand" class="demo-form-inline">
        <el-form-item label="当前状态">
            <el-select v-model="brand.status" placeholder="启用/禁用">
                <el-option label="启用" value="1"></el-option>
                <el-option label="禁用" value="0"></el-option>
            </el-select>
        </el-form-item>
        <el-form-item label="企业名称">
            <el-input v-model="brand.companyName" placeholder="企业名称"></el-input>
        </el-form-item>
        <el-form-item label="品牌名称">
            <el-input v-model="brand.brandName" placeholder="品牌名称"></el-input>
        </el-form-item>
        <el-form-item>
            <el-button type="primary" @click="selectByCondition">查询</el-button>
        </el-form-item>
    </el-form>

    <!--按钮组件-->
    <el-row>
        <el-button type="danger" plain @click="deleteBrands">批量删除</el-button>
<!--        <template>-->
<!--            <el-button type="text" @click="deleteBrands">批量删除</el-button>-->
<!--        </template>-->
        <el-button type="primary" plain @click="dialogVisible = true">新增</el-button>
    </el-row>

    <!--对话框组件-->
    <el-dialog
            title="编辑新增品牌信息"
            :visible.sync="dialogVisible"
            width="30%">
        <el-form ref="form" :model="brand" label-width="80px">
            <el-form-item label="品牌名称">
                <el-input v-model="brand.brandName"></el-input>
            </el-form-item>
            <el-form-item label="企业名称">
                <el-input v-model="brand.companyName"></el-input>
            </el-form-item>
            <el-form-item label="排序">
                <el-input v-model="brand.ordered"></el-input>
            </el-form-item>
            <el-form-item label="描述">
                <el-input type="textarea" v-model="brand.description"></el-input>
            </el-form-item>
            <!--            <el-form-item label="当前状态">-->
            <!--                <el-select v-model="brand.status" placeholder="当前状态">-->
            <!--                    <el-option label="启用" value="1"></el-option>-->
            <!--                    <el-option label="禁用" value="0"></el-option>-->
            <!--                </el-select>-->
            <!--            </el-form-item>-->
            <el-form-item label="当前状态">
<!--                <el-switch v-model="brand.status"-->
<!--                           active-color="#13ce66"-->
<!--                           inactive-color="#ff4949"-->
<!--                           active-value="1"-->
<!--                           inactive-value="0"></el-switch>-->
                <el-switch
                        v-model="brand.status"
                        active-text="启用"
                        inactive-text="禁用"
                        active-value="1"
                        inactive-value="0">
                </el-switch>
            </el-form-item>
            <el-form-item>
                <el-button type="primary" @click="addBrand">提交</el-button>
                <el-button @click="dialogVisible = false">取消</el-button>
            </el-form-item>
        </el-form>
    </el-dialog>

    <!--表格组件-->
    <template>
        <el-table
                :data="tableData"
                style="width: 100%"
                :row-class-name="tableRowClassName"
                @selection-change="handleSelectionChange">
            <el-table-column
                    type="selection"
                    width="55">
            </el-table-column>
            <el-table-column
                    type="index"
                    width="50">
            </el-table-column>

            <el-table-column
                    prop="brandName"
                    label="品牌名称"
                    align="center">
            </el-table-column>
            <el-table-column
                    prop="companyName"
                    label="企业名称"
                    align="center">
            </el-table-column>
            <el-table-column
                    prop="ordered"
                    label="排序"
                    align="center">
            </el-table-column>
            <el-table-column
                    prop="statusStr"
                    label="当前状态"
                    align="center">
            </el-table-column>
            <el-table-column
                    label="操作"
                    align="center">
                <template slot-scope="scope">
                    <el-button
                            size="mini"
                            @click="getBrandById(scope.$index, scope.row)">修改</el-button>
                    <el-button
                            size="mini"
                            type="danger"
                            @click="deleteBrand(scope.$index, scope.row)">删除</el-button>
                </template>
<!--                <el-row>-->
<!--                    <el-button type="primary" @click="updateBrand">修改</el-button>-->
<!--                    <el-button type="danger" @click="deleteBrand">删除</el-button>-->
<!--                </el-row>-->
            </el-table-column>
        </el-table>
    </template>
    <el-dialog
            title="修改品牌信息"
            :visible.sync="updateDialogVisible"
            width="30%">
        <el-form ref="form" :model="brand" label-width="80px">
            <el-form-item label="品牌名称">
                <el-input v-model="brand.brandName"></el-input>
            </el-form-item>
            <el-form-item label="企业名称">
                <el-input v-model="brand.companyName"></el-input>
            </el-form-item>
            <el-form-item label="排序">
                <el-input v-model="brand.ordered"></el-input>
            </el-form-item>
            <el-form-item label="描述">
                <el-input type="textarea" v-model="brand.description"></el-input>
            </el-form-item>
            <!--            <el-form-item label="当前状态">-->
            <!--                <el-select v-model="brand.status" placeholder="当前状态">-->
            <!--                    <el-option label="启用" value="1"></el-option>-->
            <!--                    <el-option label="禁用" value="0"></el-option>-->
            <!--                </el-select>-->
            <!--            </el-form-item>-->
            <el-form-item label="当前状态">
                <el-switch
                        v-model="brand.status"
                        active-text="启用"
                        inactive-text="禁用"
                        active-value='1'
                        inactive-value='0'>
                </el-switch>
            </el-form-item>
            <el-form-item>
                <template slot-scope="scope">
                    <el-button type="primary" @click="updateBrand">提交</el-button>
                    <el-button @click="updateDialogVisible = false">取消</el-button>
                </template>
            </el-form-item>
        </el-form>
    </el-dialog>

    <!--分页工具条-->
    <el-pagination
            @size-change="handleSizeChange"
            @current-change="handleCurrentChange"
            :current-page="currentPage"
            :page-sizes="[5, 10, 15, 20]"
            :page-size="5"
            layout="total, sizes, prev, pager, next, jumper"
            :total="totalCount">
    </el-pagination>
</div>

<script src="../js/axios-0.18.0.js"></script>
<script src="../js/vue.js"></script>
<script src="../element-ui/lib/index.js"></script>
<link rel="stylesheet" href="../element-ui/lib/theme-chalk/index.css">
<script>
    new Vue({
        el:"#app",
        mounted(){
            // this.SelectAll();
            this.selectByPage();
        },
        methods: {
            //查询所有品牌数据
            SelectAll() {
                let _this = this;//用一个_this指向当前Vue类
                axios.get("http://localhost/JavaWeb_FinalCase/brand/selectAll")
                    .then(function (resp) {
                        _this.tableData = resp.data;//然后此处的_this才会调用到Vue内的tableData模型
                    })
            },
            //分页+条件查询
            selectByPageAndCondition(){
                let _this = this;
                axios({
                    method:"POST",
                    url:"http://localhost/JavaWeb_FinalCase/brand/selectByPageAndCondition?currentPage="+this.currentPage+"&PageSize="+this.PageSize,
                    data: this.brand
                }).then(function (resp) {
                    _this.tableData = resp.data.rows;//然后此处的_this才会调用到Vue内的tableData模型
                    _this.totalCount = resp.data.totalCount;
                })
            },
            //分页查询
            selectByPage(){
                // let _this = this;//用一个_this指向当前Vue类
                // axios.get("http://localhost/JavaWeb_FinalCase/brand/selectByPage?currentPage="+_this.currentPage+"&PageSize="+_this.PageSize)
                //     .then(function (resp) {
                //         _this.tableData = resp.data.rows;//然后此处的_this才会调用到Vue内的tableData模型
                //         _this.totalCount = resp.data.totalCount;
                //     })
                axios.get("http://localhost/JavaWeb_FinalCase/brand/selectByPage?currentPage="+this.currentPage+"&PageSize="+this.PageSize)
                    .then(resp=>{//箭头的存在使得不需要再额外声明 _this 可直接使用this
                        this.tableData = resp.data.rows;//然后此处的_this才会调用到Vue内的tableData模型
                        this.totalCount = resp.data.totalCount;
                    })
            },
            handleSizeChange(val) {
                // console.log(`每页 ${val} 条`);
                //重新设置每页条目数
                this.PageSize = val;
                this.selectByPage();
            },
            handleCurrentChange(val) {
                // console.log(`当前页: ${val}`);
                //重新设置当前页码
                this.currentPage = val;
                this.selectByPage();
            },
            //点击查询按钮后进行的方法
            selectByCondition() {
                this.selectByPageAndCondition();
            },
            //批量删除
            deleteBrands(){
                this.$confirm('此操作将删除这些数据, 是否继续?', '提示', {
                    confirmButtonText: '确定',
                    cancelButtonText: '取消',
                    type: 'warning'
                }).then(() => {
                    for (let i = 0; i < this.multipleSelection.length; i++) {
                        this.selectIds[i] = this.multipleSelection[i].id
                    }
                    let _this = this;
                    axios.post("http://localhost/JavaWeb_FinalCase/brand/deleteByIds", _this.selectIds)
                        .then(function (resp) {
                            if (resp.data === "success") {
                                //重新查询所有数据-双向数据绑定-此时页面数据模型tableData改变-页面显示内容跟着改变
                                _this.selectByPage();
                            }
                        })
                    this.$message({
                        type: 'success',
                        message: '操作成功!'
                    });
                }).catch(() => {
                    this.$message({
                        type: 'info',
                        message: '已取消删除'
                    });
                });
            },
            //复选框选中后执行的方法
            handleSelectionChange(val) {
                this.multipleSelection = val;
                console.log(this.multipleSelection);
            },
            //点击删除后执行的方法
            deleteBrand(index, row) {
                //弹出确认对话框
                this.$confirm('此操作将删除该数据, 是否继续?', '提示', {
                    confirmButtonText: '确定',
                    cancelButtonText: '取消',
                    type: 'warning'
                }).then(() => {
                    let _this = this;
                    axios.get("http://localhost/JavaWeb_FinalCase/brand/delete?id=" + row.id)
                        .then(function (resp) {
                            if (resp.data === "success") {
                                //重新查询所有数据-双向数据绑定-此时页面数据模型tableData改变-页面显示内容跟着改变
                                _this.selectByPage();
                                //弹出删除成功的弹窗
                                _this.SuccessMessage();
                            }
                        })
                }).catch(() => {
                    this.$message({
                        type: 'info',
                        message: '已取消删除'
                    });
                });

            },
            //点击修改品牌信息按钮的执行方法
            getBrandById(index, row) {
                //先获取要修改的品牌信息
                // let _this = this;
                // axios.get("http://localhost/JavaWeb_FinalCase/brand/selectById?id=" + row.id)
                //     .then(function (resp) {
                //         _this.brand = resp.data;
                //     })
                this.brand = row;
                this.updateDialogVisible = true;
            },
            //修改并确认提交至服务器的方法
            updateBrand() {
                let _this = this;
                axios.post("http://localhost/JavaWeb_FinalCase/brand/update", _this.brand)
                    .then(function (resp) {
                        if (resp.data === "success") {
                            //关闭弹窗
                            _this.updateDialogVisible = false;
                            //重新查询所有数据-双向数据绑定-此时页面数据模型tableData改变-页面显示内容跟着改变
                            _this.selectByPage();
                            //弹出修改成功的弹窗
                            _this.SuccessMessage();
                        }
                    })
            },
            //新增内表单提交后执行的方法
            addBrand() {
                let _this = this;
                axios.post("http://localhost/JavaWeb_FinalCase/brand/add", _this.brand)
                    .then(function (resp) {
                        if (resp.data === "success") {
                            // location.href = "http://localhost/JavaWeb_FinalCase/html/brand.html?"+new Date().getMilliseconds();
                            //关闭弹窗
                            _this.dialogVisible = false;
                            //重新查询所有数据-双向数据绑定-此时页面数据模型tableData改变-页面显示内容跟着改变
                            _this.selectByPage();
                            //弹出添加成功的弹窗
                            _this.SuccessMessage();
                        }
                    })
            },
            SuccessMessage() {
                this.$message({
                    message: '此操作成功!',
                    type: 'success',
                    center: true
                });
            },
            tableRowClassName({row, rowIndex}) {
                if (rowIndex === 1) {
                    return 'warning-row';
                } else if (rowIndex === 3) {
                    return 'success-row';
                }
                return '';
            },
        },
        data() {
            return {
                //总记录数
                totalCount: 100,
                //当前页码
                currentPage: 1,
                //每页条目数
                PageSize: 5,
                //新增对话框是否显示标记
                dialogVisible: false,
                //修改对话框是否显示标记
                updateDialogVisible: false,
                //品牌模型数据
                brand: {
                    brandName: '',
                    companyName: '',
                    ordered: '',
                    description: '',
                    id: '',
                    status: ''
                },
                //复选框选中数据集合
                multipleSelection: [],
                //批量删除被选中的ids
                selectIds: [],
                //表格数据
                tableData: [ {brandName: '华为',
                companyName: '华为科技有限公司',
                ordered: '100',
                status: "1"
        }, {
                brandName: '华为',
                    companyName: '华为科技有限公司',
                    ordered: '100',
                    status: "1"
            }, {
                brandName: '华为',
                    companyName: '华为科技有限公司',
                    ordered: '100',
                    status: "1"
            }, {
                brandName: '华为',
                    companyName: '华为科技有限公司',
                    ordered: '100',
                    status: "1"
            }]
            }
        }
    });
</script>
</body>
</html>